package QianFeng04;

/**
 * 继承的应用示例：
 * 实现一个化妆品商城中的化妆品管理
 * 1、定义一个化妆品类( Cosmetic)
 * 2、定义一一个化妆品管理类(CosmeticManger)
 * (1)实现进货功能
 * (2)可以输出所有化妆品信息功能
 * 3、使用继承实现一个可按单价排序输出所有化妆品的功能
 * 4、使用继承实现一个只输出进口化妆品的功能
 **/
public class Test2 {
    public static void main(String[] args) {
        ImportCosmeticManager cm = new ImportCosmeticManager();
        cm.add(new Cosmetic("香奈儿","进口",1200));
        cm.add(new Cosmetic("大宝","国产",500));
        cm.add(new Cosmetic("韩后","国产",900));
        cm.add(new Cosmetic("圣罗兰","进口",1000));
        cm.printInfo();
    }
}

//化妆品管理类
class CosmeticManager {
    protected Cosmetic[] cs = new Cosmetic[4];
    protected int count = 0;

    //进货功能
    public void add(Cosmetic cosmetic) {
        int size = cs.length;
        if (count >= size) {
            int newLen = size * 2;
            cs = java.util.Arrays.copyOf(cs, newLen);
        }
        cs[count] = cosmetic;
        count++;
    }
    //输出所有的产品
    public void printInfo(){
        for (int i = 0; i < count; i++) {
            System.out.println(cs[i].getInfo());
        }
    }
}

//只输出进口化妆品管理类
class ImportCosmeticManager extends CosmeticManager{
    //只输出进口的产品信息
    public void printInfo(){
        for (int i = 0; i < count; i++) {
            if (cs[i].getType().equals("进口")) {
                System.out.println(cs[i].getInfo());
            }
        }
    }
}

//可按单价排序的化妆品管理类
class SortCosmeticManager extends CosmeticManager{
    //对父类方法重写，排序输出所有的产品
    public void printInfo(){
        Cosmetic[] temp = java.util.Arrays.copyOf(cs, count);
        Cosmetic c = null;
        for (int i = 0; i < temp.length-1; i++) {
            for (int j = 0; j < temp.length - 1 - i; j++) {
                if (temp[j].getPrice() > temp[j + 1].getPrice()) {
                    c = temp[j];
                    temp[j] = temp[j + 1];
                    temp[j+1] = c;
                }
            }
        }
        for (Cosmetic cosmetic : temp) {
            System.out.println(cosmetic.getInfo());
        }
    }
}

//化妆品类
class Cosmetic {
    private String name;//品牌
    private String type;//进口或国产
    private int price;//价格
    public Cosmetic(){}
    public Cosmetic(String name, String type, int price) {
        this.name = name;
        this.type = type;
        this.price = price;
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String getInfo() {
        return "name=" + name + ",type=" + type + ",price=" + price;
    }
}
